using B.S.Data.MES.API.Applications.CangKu.Command;
using B.S.Domain.CangKu;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Applications.CangKu.CommandHandlers
{
    /// <summary>
    /// UpdateChukuCommand 的处理程序
    /// </summary>
    public class UpdateChukuCommandHandler : IRequestHandler<UpdateChukuCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<Finished_ProductTest> _finishedRepository;

        public UpdateChukuCommandHandler(
            IBaseRepository<Finished_ProductTest> finishedRepository)
        {
            _finishedRepository = finishedRepository;
        }

        public  Task<APIRequst<int>> Handle(UpdateChukuCommand request, CancellationToken cancellationToken)
        {
            var response = new APIRequst<int>();

            var db = _finishedRepository.db;
            var entity =_finishedRepository.GetAll()
                .FirstOrDefault(x => x.SID == request.SID);

            if (entity == null)
            {
                response.Code = RequstCode.失败;
                response.Msg = "未找到对应的成品记录";
                return Task.FromResult(response);
            }

            entity.ProductCount = request.ProductCount;

            var effectRows =  _finishedRepository.Update(entity);

            //if (effectRows <= 0)
            //{
            //    response.Code = RequstCode.失败;
            //    response.Msg = "更新失败";
            //    return Task.FromResult(response);
            //}

            response.Code = RequstCode.成功;
            response.Msg = "更新成功";
            return Task.FromResult(response); 
        }
    }
}

